package servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Models.Customer;
import Models.Mailler;

public class CustomerServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action") != null ? req.getParameter("action") : "";
		// Creation page for a customer
		if(action.equals("new")) {
			System.out.println("go new cust");
			getServletContext().getRequestDispatcher("/jsp/customer/new.jsp").forward(req, resp);
			
		// Show a customer
		} else if(action.equals("show")) {
			String id = req.getParameter("id");
			try {
				req.setAttribute("customer", Customer.LoadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			getServletContext().getRequestDispatcher("/jsp/customer/show.jsp").forward(req, resp);
			
		// Edit page for a customer
		} else if(action.equals("edit")) {
			String id = req.getParameter("id");
			try {
				req.setAttribute("customer", Customer.LoadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			getServletContext().getRequestDispatcher("/jsp/customer/edit.jsp").forward(req, resp);		
		// Delete a customer
		}else if(action.equals("delete")){
			String id = req.getParameter("id");
			try {
				Customer customer = Customer.LoadSql(Integer.parseInt(id));
				Customer.delete(customer);				
			} catch (NumberFormatException e) {e.printStackTrace();}
		
			req.setAttribute("list_customers", Customer.LoadSql());
			getServletContext().getRequestDispatcher("/jsp/customer/index.jsp").forward(req, resp);
		// Search page for customer
		}else if(action.equals("search")) {
			getServletContext().getRequestDispatcher("/jsp/customer/search.jsp").forward(req, resp);
		}else if(action.equals("email")) {
			String id = "";
			if(req.getParameterMap().containsKey("id"))
				id = req.getParameter("id");
			else if(req.getParameterMap().containsKey("ids"))
				id = req.getParameter("ids");
			
			req.setAttribute("id", id);
			getServletContext().getRequestDispatcher("/jsp/customer/email.jsp").forward(req, resp);	
			//index
		}else{
			req.setAttribute("list_customers", Customer.LoadSql());
			getServletContext().getRequestDispatcher("/jsp/customer/index.jsp").forward(req, resp);				
		}
	}	

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String action = req.getParameter("action") != null? req.getParameter("action") : "";
		
		if(action.equals("new")){
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			Customer cust = new Customer();
			cust.setName(req.getParameter("first_name"));
			cust.setLastName(req.getParameter("last_name"));
			cust.setPhoneNumber(req.getParameter("phone"));
			try {
				cust.setBirthDate(format.parse(req.getParameter("birth_date")));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			cust.setAddress(req.getParameter("address"));
			cust.setEmailAdress(req.getParameter("email"));
			cust.setCity(req.getParameter("city"));
			cust.setCountry(req.getParameter("country"));
			cust.setNotes(req.getParameter("notes"));
			Customer.Save(cust);
			
			res.sendRedirect("/SiteERP/customer/");
			
		} else if(action.equals("edit")) {
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
			int id = Integer.parseInt(req.getParameter("id"));
			Customer customer = Customer.LoadSql(id);
			customer.setName(req.getParameter("first_name"));
			customer.setSurname(req.getParameter("last_name"));
			customer.setPhoneNumber(req.getParameter("phone"));
			try {
				customer.setBirthDate(format.parse(req.getParameter("birth_date")));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			customer.setAddress(req.getParameter("address"));
			customer.setCity(req.getParameter("city"));
			customer.setCountry(req.getParameter("country"));
			customer.setNotes(req.getParameter("notes"));
			Customer.Update(customer);
			
			getServletContext().getRequestDispatcher("/jsp/customer/index.jsp").forward(req, res);
		// Search a customer
		} else if(action.equals("search")){
			List<Customer> list_customers = new ArrayList<Customer>();
			list_customers = Customer.search(req.getParameter("first_name"),
					req.getParameter("last_name"), req.getParameter("start_age"), 
					req.getParameter("end_age"), req.getParameter("city"), 
					req.getParameter("country"));
			String ids = "";
			for(Customer c : list_customers){
				ids += c.getId()+";";
			}
			
			req.setAttribute("ids", ids);
			req.setAttribute("list_customers", list_customers);
			getServletContext().getRequestDispatcher("/jsp/customer/result.jsp").forward(req, res);
			//doGet(req, res);
		// Index
		} else if(action.equals("email")) {
			String ids = req.getParameter("id");
			String title = req.getParameter("title");
			String content = req.getParameter("content");
			
			if(ids != null){
				String[] tmp = ids.split(";");
				for(String id : tmp){
					if(id.equals("")) continue;
					Customer c = null;
					try { c = new Customer(Integer.parseInt(id)); }
					catch (NumberFormatException e) {}
					if(c == null) continue;
					
					Mailler.sendMail(title, content, c.getEmailAddress());
				}	
			}
			
			doGet(req, res);
		} else {
			doGet(req, res);
		}
	}

}
